/**
 * Copyright (c) Css Team
 * All rights reserved.
 *
 * This file DictMan.java creation date: [Jan 13, 2014 15:21:03 PM] by liuzhb
 * http://www.css.com.cn
 */
package com.css.apps.base.dict.action;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.dict.model.SDict;
import com.css.apps.base.dict.service.QueryDict;
import com.css.db.query.QueryCache;
import com.css.util.Ajax;
import com.css.util.Json;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.AbstractAction;
import com.opensymphony.xwork.Action;

@SuppressWarnings("serial")
public class GetDictsForDesigner extends AbstractAction {
	
	private static Log log = LogFactory.getLog(GetDictsForDesigner.class);
	private String tableName = null;

	public String go() {
		try {
			if (StringHelper.isEmpty(tableName))
				tableName = "d_root";
			QueryCache qc = new QueryDict(" select a.uuid from SDict a " + getWhere() + getOrder());
			setWhere(qc);
			List<?> resList = qc.idToObject(SDict.class, qc.listCache());
			result = Ajax.JSONResult(0, Json.list2json(resList));
			return Action.SUCCESS;
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			result = Ajax.JSONResult(1, Messages.getString("systemMsg.dbFaild"));
			setMessage(Messages.getString("systemMsg.dbFaild"));
			return Action.ERROR;
		}
	}

	public String getWhere() {
		StringBuffer sb = new StringBuffer(" where 1=1 ");
		if (StringHelper.isNotEmpty(tableName))
			sb.append(" and a.tableName = :tableName ");
		return sb.toString();
	}

	public void setWhere(QueryCache qc) {
		if (StringHelper.isNotEmpty(tableName))
			qc.setParameter("tableName", tableName);
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public static String getOrder() {
		return " order by a.code";
	}
}
